package com.hqyj.second.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hqyj.second.domain.Users;
import com.hqyj.second.mapper.UsersMapper;
import com.hqyj.second.repository.UsersRepository;
import com.hqyj.second.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper,Users> implements UsersService {


    @Value("${cruddemoproject.perPageSize}")
    private Integer perPageSize;
    @Resource
    //@Autowired
    private UsersRepository usersRepository;

    @Resource
    private UsersMapper usersMapper;

    @Override
    public Users saveUser(Users u) throws Exception {

        return usersRepository.save(u);
    }

    @Override
    public Users findUserByUid(Integer uid) {
        Optional<Users> optional = usersRepository.findById(uid);
        return optional.orElse(null);
    }

    @Override
    public List<Users> findAllUsers() {
        List<Users> usersList = usersRepository.findAll();
        return usersList;
    }

    @Override
    public Users updateUser(Users u) throws Exception {
        return usersRepository.save(u);
    }

    @Override
    public boolean deleteUser(Integer uid) throws Exception {
        try {
            usersRepository.deleteById(uid);
            return true;
        } catch (Exception ex) {
            return false;
        }
    }

    @Override
    public boolean insertUser(Users u) throws Exception {
        return super.save(u);
    }

    @Override
    public Users queryUserByUid(Integer uid) {
        return super.getById(uid);
    }

    @Override
    public List<Users> queryAllUsers() {
        return super.list(null);
    }

    @Override
    public boolean modifyUser(Users u) throws Exception {
        return super.updateById(u);
    }

    @Override
    public boolean removeUser(Integer uid) throws Exception {
        return super.removeById(uid);
    }


    @Override
    public boolean removeBatchUsers(List<Integer> ids) throws Exception {
        return super.removeBatchByIds(ids);
    }

    @Override
    public Users login(String username, String password) {
        QueryWrapper<Users> queryWrapper = new QueryWrapper<Users>();
        queryWrapper.eq("username",username);
        queryWrapper.eq("password",password);
        Users u = super.getOne(queryWrapper);
        if(u!=null){
            return u; //表示登录成功用户对象；
        }
        return null;
    }

    @Override
    public Page<Users> queryUsersPager(Integer pageNumber) {
        //mybatis-plus如何实现分页呢？

        Page<Users> page = new Page<Users>(pageNumber, perPageSize);
        usersMapper.selectPage(page,null);

        return page;
    }
}
